<template>
    <div>
        <Card>
            <!-- search start -->
            <div class="search">
                <Form :model="search" class="search_box" inline label-position="right">
                    <!-- <FormItem label="分部：">
                        <Select class="option" v-model="search.branchid">
                            <Option v-for="item in branchList " :value="item.value" :key="item.value">{{ item.label }}</Option>
                        </Select>
                    </FormItem> -->

                    <FormItem label="发起人：">
                        <Input v-model="search.from_username" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="当前处理人：">
                        <Input v-model="search.deal_username" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="名称：">
                        <Input v-model="search.keywords" class="FormItemInput"></Input>
                    </FormItem>

                    <FormItem label="事项状态：">
                        <Select class="option" v-model="search.status">
                            <Option v-for="item in statusList " :value="item.id" :key="item.id">{{ item.value }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="来源：">
                        <Select class="option" v-model="search.source">
                            <Option v-for="item in originList " :value="item.id" :key="item.id">{{ item.value }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="当前处理人是否超时：">
                        <Select class="option" v-model="search.is_out_time">
                            <Option v-for="item in overTimeList " :value="item.id" :key="item.id">{{ item.value }}</Option>
                        </Select>
                    </FormItem>

                    <FormItem label="时间：">
                        <DatePicker style="width:300px" type="datetimerange" placeholder="Select date and time" @on-change="changeTime($event)"></DatePicker>
                    </FormItem>


                    <FormItem>
                        <Button icon="md-search" type="primary" @click="searchList">搜索</Button>
                        <Button v-if="role < 3" style="margin-left:10px" icon="ios-download-outline" type="success" @click="exportExcel">导出Excel</Button>
                    </FormItem>
                </Form>

            </div> <!-- end search -->

            <Table ref="table" :loading="loading" stripe :columns="columns1" :data="data1"></Table>
            <Page :total="total" :current="search.page" show-sizer show-total :page-size="search.rows" @on-page-size-change="getListByPageSize" @on-change="getListByPageNum"> </Page>
        </Card>

    </div>
</template>

<script>
    import axios from '@/libs/api.request'
    import {
        exportExcel
    } from '@/api/data'
    import {
        mapMutations,
        mapGetters
    } from 'vuex'
    export default {
        name: 'matter-page',
        data() {
            return {
                loading: false,

                // 搜索对象
                search: {
                    from_username: '',
                    deal_username: '',
                    keywords: '',
                    status: -1,
                    source: -1,
                    is_out_time: -1,
                    start_time: '',
                    end_time: '',
                    page: 1,
                    rows: 10
                },

                // 状态列表
                statusList: [{
                        id: -1,
                        value: '全部'
                    },
                    {
                        id: 2,
                        value: '进行中'
                    },
                    {
                        id: 1,
                        value: '已完成'
                    }
                ],
                // 来源列表
                originList: [{
                        id: -1,
                        value: '全部'
                    },
                    {
                        id: 1,
                        value: '巡店'
                    },
                    {
                        id: 2,
                        value: '协同'
                    },
                ],
                // 超时列表
                overTimeList: [{
                        id: -1,
                        value: '全部'
                    },
                    {
                        id: 0,
                        value: '未超时'
                    },
                    {
                        id: 1,
                        value: '已超时'
                    },
                ],

                // 数据总条数
                total: 0,

                columns1: [{
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '发起人',
                        align: 'center',
                        key: 'from_username'
                    },
                    {
                        title: '发起时间',
                        align: 'center',
                        key: 'c_time'
                    },
                    {
                        title: '名称',
                        align: 'center',
                        key: 'name'
                    },
                    {
                        title: '来源',
                        align: 'center',
                        key: 'source_zw'
                    },
                    {
                        title: '当前处理人',
                        align: 'center',
                        key: 'deal_username'
                    },
                    {
                        title: '状态',
                        align: 'center',
                        width: 100,
                        render: (h, params) => {
                            return h('span', {
                                style: {
                                    color: params.row.status == 2 ? '#FF9800' : '#2BDE73 '
                                }
                            }, params.row.status_zw)
                        },
                    },
                    {
                        title: '当前处理人是否超时：',
                        width: 100,
                        align: 'center',
                        render: (h, params) => {
                            return h('span', {
                                style: {
                                    color: params.row.out_time_zw == '已超时' ? '#F2555B ' : '#2BDE73 '
                                }
                            }, params.row.out_time_zw)
                        },
                    },
                    {
                        title: '处理动态',
                        align: 'center',
                        width: 300,
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'default',
                                        icon: 'ios-eye'
                                    },
                                    style: {
                                        marginRight: '14px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.myClose('matter-detail')
                                            this.$_changePage('matter-detail', '事项详情', {
                                                matterid: params.row.id
                                            })
                                        }
                                    }
                                }, '查看详情'),
                                h('Button', {
                                    props: {
                                        type: 'warning',
                                        size: 'default',
                                        icon: 'ios-eye'
                                    },
                                    style: {
                                        marginRight: '14px',
                                        height: '30px'
                                    },
                                    on: {
                                        click: () => {
                                            this.myClose('mistake-matter')
                                            this.$_changePage('mistake-matter', '事项明细', {
                                                flag: params.row.flag
                                            })
                                        }
                                    }
                                }, '超时详情'),

                            ])
                        }
                    }
                ],
                data1: []
            }
        },
        methods: {
            ...mapMutations(['myClose']),
            ...mapGetters(['getRole']),

            // 获取事项列表
            getMatterList() {
                this.loading = true
                let {
                    ...data
                } = this.search
                axios.ajax_post('admin-matter-ajax_get_matter', data).then(res => {
                    this.loading = false
                    if (res.data.code === 1) {
                        this.data1 = res.data.data.list
                        this.total = res.data.data.total_num

                        if (this.data1.length < 1 && this.total > 0) {
                            this.search.page = Math.ceil(this.total / this.search.rows)
                            this.getMatterList()
                        }
                    } else {
                        this.$Notice.error({
                            title: res.data.msg,
                            duration: 3
                        })
                    }
                })

            },

            // 导出
            exportExcel() {
                exportExcel('admin-matter-export_matter', this.search)
            },

            // 格式化时间
            changeTime(value) {
                this.search.start_time = value[0]
                this.search.end_time = value[1]
            },

            // 搜索
            searchList() {
                this.search.page = 1
                this.search.rows = 10
                this.getMatterList()
            },

            getListByPageSize(rows) {
                this.search.rows = rows
                this.getMatterList()
            },

            getListByPageNum(page) {
                this.search.page = page
                this.getMatterList()
            },
        },
        computed: {
            role() {
                return this.getRole()
            }
        },
        created() {
            this.getMatterList()
        }
    }
</script>

<style lang='less' scoped>
    .search {
        .search_box {
            display: flex;
            flex-wrap: wrap;

            .option {
                width: 80px;
            }

            .ivu-form-item {
                margin-right: 12px;
                display: flex;
                justify-content: flex-start;
                margin-bottom: 20px;
            }
        }
    }

    .FormItemInput {
        width: 100px;
    }
</style>